from langchain.agents import Tool, create_react_agent, AgentExecutor
from langchain_community.llms.tongyi import Tongyi
from langchain_core.prompts import ChatPromptTemplate


def search_order(user_input):
    print('订单号', user_input)
    return '订单号' + '000000'


def recommend_goods(user_input):
    print('商品推荐', user_input)
    return '商品推荐' + '000000'


def fake_news(user_input):
    print('新闻', user_input)
    return '新闻' + '000000'


tools = [
    Tool(
        name="search_order",
        description="用于查询订单号",
        func=search_order
    ),
    Tool(
        name="recommend_goods",
        description="用于推荐商品",
        func=recommend_goods
    ),
    Tool(
        name="fake_news",
        description="用于查询新闻",
        func=fake_news
    )
]

template = """
Answer the following questions as best you can. You have access to the following tools,请不要添加自己的回答,只返回工具返回内容即可,不要添加自己的文字:

            {tools}

            Use the following format:

            Question: the input question you must answer
            Thought: you should always think about what to do
            Action: the action to take, should be one of [{tool_names}]
            Action Input: the input to the action
            Observation: the result of the action
            ... (this Thought/Action/Action Input/Observation can repeat N times)
            Thought: I now know the final answer
            Final Answer: the final answer to the original input question

            Begin!

            Question: {input}
            Thought:{agent_scratchpad}
"""
prompt_template = ChatPromptTemplate.from_template(template)
agent = create_react_agent(
    tools=tools,
    prompt=prompt_template,
    llm=Tongyi()
)

agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    verbose=True,
)

res = agent_executor.invoke(
    {"input":"查询订单","chat_history":[]}
)
print(res)